<?php
/*
---------------------------------------------------------------------------
admin/changepassword.php for SALAM Frontend

Copyright 2009, 2010 Jacob McEntire

    This file is part of SALAM.

    SALAM is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SALAM is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SALAM.  If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------
*/
require("../../library/frontend.php");

page_start('Change Password', TRUE);

if (isset($_POST['password'])) {
	if ($_POST['password'] == $_POST['confirm']) {
		$pass = crypt(trim($_POST['password']),base64_encode(CRYPT_STD_DES));
		$file = ".htpasswd";
		$data = 'admin:' . $pass;
		if (is_writable($file)){
			if (!$handle = fopen($file, 'w'))
				echo 'Cannot open file ', $file;
			elseif (fwrite($handle, $data) === FALSE)
				echo 'Cannot write to file ', $htpasswd_file;
			else
				echo 'Password Saved Successfully! <a href="hosts.php">Back to Administration Mode</a><br />';
			fclose($handle);
		}
		else
			echo $file, ' is not writeable';
	}
	else
		echo 'Passwords Do Not Match. <a href="changepassword.php">Try again</a>';
}
else {
	echo '
	<form method="post" action="changepassword.php" enctype="multipart/form-data">
	<table width="500" border="0" cellpadding="1" cellspacing="1">
	<tr><th colspan=2>Administration Password:</th></tr>
	<tr><td align="right">Password:</td><td><input type="password" name="password" /></td></tr>
	<tr><td align="right">Confirm:</td><td><input type="password" name="confirm" /></td></tr>
	<tr><td colspan="2" align="center"><input name="save" type="submit" class="box" value=" Save "></td></tr>
	</table>
	</form>';
}
page_end();
?>